/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-05-15     
 */
package com.task.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.task.bean.Constants;
import com.task.entity.User;
import com.task.repository.IUserRepository;
import com.task.service.IUserService;
import com.task.utils.PageUtil;

/**
 * 用户服务
 */
@Service
public class UserServiceImpl implements IUserService
{
    
    @Autowired
    IUserRepository userRepository;
    
    @Override
    public void save(User user)
    {
        userRepository.save(user);
    }
    
    @Override
    public void delete(Long id)
    {
        userRepository.delete(id);
    }
    
    @Override
    public User findById(Long id)
    {
        return userRepository.findOne(id);
    }
    
    @Override
    public User findByOpenId(Long openId)
    {
        Map<String, Object> searchParams = new HashMap<String, Object>();
        searchParams.put("EQ_openId", openId);
        Specification<User> spec = PageUtil.buildSpecification(searchParams, User.class);
        return userRepository.findOne(spec);
        
    }
    
    @Override
    public Page<User> findUserPage(Map<String, Object> searchParams, int page, int rows)
    {
        PageRequest pageRequest = PageUtil.buildPageRequest(page, rows, Constants.DESC, Constants.ID);
        Specification<User> spec = PageUtil.buildSpecification(searchParams, User.class);
        return userRepository.findAll(spec, pageRequest);
    }
    
    @Override
    public List<User> findByName(String name)
    {
        Map<String, Object> searchParams = new HashMap<String, Object>();
        searchParams.put("LIKE_name", name);
        Specification<User> spec = PageUtil.buildSpecification(searchParams, User.class);
        return userRepository.findAll(spec);
    }

	@Override
	public User findByUserId(String userId) {
		return userRepository.findByUserId(userId);
	}
    
}
